package com.cy.pj.sys.controller;

import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RequestMapping("/user/")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @RequestMapping("doFindPageObjects")
    public JsonResult doFindPageObjects(String username, Long pageCurrent){
        return new JsonResult(sysUserService.findPageObjects(username, pageCurrent));
    }


    @RequestMapping("doValidById")
    public JsonResult doValidById(Integer id, Integer valid){
        sysUserService.validById(id, valid);
        return new JsonResult("Update OK");
    }

    @RequestMapping("doSaveObject")
    public JsonResult doSaveObject(SysUser sysUser, Integer [] roleIds){
        sysUserService.saveObject(sysUser, roleIds);
        return new JsonResult("Save OK");
    }


    @RequestMapping("doFindObjectById")
    public JsonResult doFindObjectById(Integer id){
        Map<String,Object> map=
                sysUserService.findObjectById(id);
        return new JsonResult(map);
    }



    @RequestMapping("doUpdateObject")
    public JsonResult doUpdateObject(SysUser sysUser, Integer[] roleIds){
        sysUserService.updateObject(sysUser, roleIds);
        return new JsonResult("Update OK");
    }


    @RequestMapping("doUpdatePassword")
    public JsonResult doUpdatePassword(String password, String newPassword, String cfgPassword){
        sysUserService.updatePassword(password, newPassword, cfgPassword);
        return new JsonResult("Update OK");
    }


    @RequestMapping("doLogin")
    public JsonResult doLogin(String username, String password, boolean isRememberMe){
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=
                new UsernamePasswordToken(username,password);
        if(isRememberMe){
            token.setRememberMe(true);
        }
        token.setUsername(username);
        token.setPassword(password.toCharArray());
        subject.login(token);
        return new JsonResult("login ok");
    }

}
